package com.farmshop.www.swiperefreshlayoutdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.farmshop.www.swiperefreshlayoutdemo.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/3/30.
 * 创建时间：2017/3/30  14:33
 * 项目名称：MsgTrasmit
 * 创建人：PeterZhang
 */

public class mAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context context;
    ArrayList<String> arrayList;
    OnItemClickListener mOnItemClickListener;
    OnItemLongClickListener mOnItemLongClickListener;

    public mAdapter(Context context,ArrayList<String> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        mViewHolder holder = new mViewHolder(LayoutInflater.from(
                context).inflate(R.layout.item, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final mViewHolder mholder = (mViewHolder) holder;
        mholder.tv.setText(arrayList.get(position));
        //判断是否设置了监听器
        if (mOnItemClickListener != null) {
            //为ItemView设置监听器
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = mholder.getLayoutPosition(); // 1
                    mOnItemClickListener.onItemClick(mholder.itemView, position); // 2
                }
            });
        }
        if (mOnItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemLongClickListener.onItemLongClick(holder.itemView, position);
                    //返回true 表示消耗了事件 事件不会继续传递
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }
    class mViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        View.OnClickListener onClickListener;
        TextView tv;
        ImageView imageView;

        public mViewHolder(View itemView, View.OnClickListener onClickListener) {
            super(itemView);
            this.onClickListener = onClickListener;
            itemView.setOnClickListener(this);
        }


        public mViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.image_list);
            tv = (TextView) itemView.findViewById(R.id.tv_list);
        }

        @Override
        public void onClick(View v) {
            Toast.makeText(context,"点击",Toast.LENGTH_LONG).show();
/*            if(onClickListener != null){
                onClickListener.onClick(v);
            }*/
        }
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    public interface OnItemLongClickListener{
        void onItemLongClick(View view,int position);
    }
}
